package com.google.code.drift.adapter;

import java.util.ArrayList;

import com.google.code.drift.api.ChatMessage;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class ChatRoomContentAdapter extends BaseAdapter {

	private ArrayList<ChatMessage> chatMessageList = new ArrayList<ChatMessage>();
	
	private Context context;
	
	public ChatRoomContentAdapter(Context context, ArrayList<ChatMessage> chatMessageList) {
		this.chatMessageList = chatMessageList;
		this.context = context;
	}
	
	@Override
	public int getCount() {
		return chatMessageList.size();
	}

	@Override
	public Object getItem(int position) {
		return chatMessageList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ChatMessage cm = chatMessageList.get(position);
		if (cm.isMe()) {
			if (convertView == null) {
				convertView = new ChatItemRight(context);
			} else if (convertView instanceof ChatItemLeft) {
				convertView = new ChatItemRight(context);
			}
			((ChatItemRight) convertView).show(cm);
		} else {
			if (convertView == null) {
				convertView = new ChatItemLeft(context);
			} else if (convertView instanceof ChatItemRight) {
				convertView = new ChatItemLeft(context);
			}
			((ChatItemLeft) convertView).show(cm);
		}
		return convertView;
	}

}
